package com.ctim.dao.user;

import com.ctim.model.user.Role;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * copyright © 2008-2016 CTIM. All Right Reserved.
 * Created by jonnyLee on 2016/9/5.
 * Desc:
 */
@Repository
public class UserRoleDao {
    @Autowired
    @Qualifier("sqlSession")
    private SqlSessionTemplate sqlSession;


    public void insertUserRole(Integer userId, String[] roleIds) {
        Map<String, Object> map = new HashMap();
        map.put("userId", userId);
        map.put("roleIds", roleIds);
        sqlSession.insert("dta.user.insertUserRole", map);
    }


    public void updateUserRole(Integer userId, String[] roleIds) {
        Map<String, Object> map = new HashMap();
        map.put("userId", userId);
        map.put("roleIds", roleIds);
        sqlSession.delete("dta.user.deleteUserRoles", userId);
        this.insertUserRole(userId, roleIds);
    }

    public List<Role> selectRoles(Integer userId) {
        return sqlSession.selectList("dta.user.selectRoles", userId);
    }
}
